package com.maddox.relax.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.maddox.relax.R;
import com.maddox.relax.model.ImageInfo;
import com.maddox.relax.util.LogUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class PhotoPickerAdapter extends RecyclerView.Adapter<PhotoPickerAdapter.ViewHolder> {


    private Context context;
    private List<ImageInfo> datas;
    private onItemClickListener itemClickListener;
    private Set<ImageInfo> selectImages = new HashSet<>();
    private boolean isMultiple = false; //用来判断是不是多选


    public PhotoPickerAdapter(Context context, List<ImageInfo> datas) {
        this.context = context;
        this.datas = datas;
        if (this.datas == null) {
            this.datas = new ArrayList<>();
        }
    }

    public List<ImageInfo> getDatas() {
        return datas;
    }

    //相册原图集合
    public ArrayList<ImageInfo> getSelectDatas() {
        ArrayList<ImageInfo> sels = new ArrayList<>();
        for (ImageInfo selectImage : selectImages) {
            sels.add(selectImage);
        }
        return sels;

    }
    public int getSelectSize(){
        return selectImages.size();
    }

    public List<File> getSelectFiles() {
        List<File> files = new ArrayList<>();
        for (ImageInfo selectImage : selectImages) {
            files.add(new File(selectImage.getFilePath()));
        }
        return files;
    }

    public void addData(List<ImageInfo> list) {
        if (list == null) {
            return;
        }
        this.datas.addAll(list);
        notifyDataSetChanged();
    }

    public void addDataNew(List<ImageInfo> list) {
        if (list == null) {
            return;
        }
        this.datas.clear();
        notifyDataSetChanged();
        this.datas.addAll(list);
        notifyDataSetChanged();

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.layout_image_picker_item, null, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ImageInfo imageInfo = datas.get(position);
        imageInfo.setSelectIndex(position);
        holder.bindData(imageInfo);
    }


    @Override
    public int getItemCount() {
        return datas.size();
    }

    public void clear() {
        datas.clear();
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView imageView;
        private CheckBox checkBox;
        private View itemView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            this.itemView = itemView;
            imageView = itemView.findViewById(R.id.picker_itemimage);
            checkBox = itemView.findViewById(R.id.picker_itemcheck);
            checkBox.setEnabled(false);
        }

        public void bindData(ImageInfo imageInfo) {
            if (imageInfo.isSelect()) {
                checkBox.setChecked(true);
            } else {
                checkBox.setChecked(false);
            }
            Glide.with(context).load(imageInfo.getUri()).placeholder(R.drawable.icon_selected_no).into(imageView);
            imageView.setOnClickListener(v -> {
                boolean select = imageInfo.isSelect();
                imageInfo.setSelect(!select);
                checkBox.setChecked(imageInfo.isSelect());
                if (imageInfo.isSelect()) {
                    if (!isMultiple) {
                        resetSlectImages();
                    }
                    //选中添加到选中组
                    selectImages.add(imageInfo);
                } else {
                    //去选中组中查看移除
                    selectImages.remove(imageInfo);
                }

            });
        }

        public void setImageClickListener(View.OnClickListener listener) {
            imageView.setOnClickListener(listener);
        }
    }

    private void resetSlectImages() {
        for (ImageInfo selectImage : selectImages) {
            selectImage.setSelect(false);
            notifyItemChanged(selectImage.getSelectIndex());
        }
        selectImages.clear();
    }

    public void setMultiple(boolean isMultiple) {
        this.isMultiple = isMultiple;
    }

    public void setOnItemClickListener(onItemClickListener listener) {
        this.itemClickListener = listener;
    }

    public interface onItemClickListener {

        void onItemClick(int position);
    }
}
